/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.inf.acesso.view.geral;

import br.inf.acesso.bancodedados.sqlserver.BDDocumentoEmp;
import br.inf.acesso.regrasdenegocios.geral.Documento;
import br.inf.acesso.regrasdenegocios.geral.Item;
import br.inf.acesso.validadores.Tabela;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Douglas
 */
public class DocumentoItem extends javax.swing.JDialog {

    private String documento;
    private String origem;
    DefaultTableModel modeloDocItem;

    /**
     * Creates new form DocumentoItem
     */
    public DocumentoItem(java.awt.Frame parent, boolean modal, String documento, String origem) {
        super(parent, modal);
        initComponents();
        this.documento = documento;
        this.origem = origem;

        buscaInfos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaItens = new javax.swing.JTable();
        txtDocumento = new javax.swing.JLabel();
        campoDocumento = new javax.swing.JTextField();
        txtVlTotal = new javax.swing.JLabel();
        campoVlTotal = new javax.swing.JTextField();
        txtOrigem = new javax.swing.JLabel();
        campoOrigem = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tabelaItens.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "N° Item", "Codigo", "Nome", "Valor Unitario", "CFOP", "Quantidade", "Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tabelaItens);
        tabelaItens.getColumnModel().getColumn(0).setPreferredWidth(10);
        tabelaItens.getColumnModel().getColumn(1).setPreferredWidth(15);
        tabelaItens.getColumnModel().getColumn(2).setPreferredWidth(250);
        tabelaItens.getColumnModel().getColumn(3).setPreferredWidth(20);
        tabelaItens.getColumnModel().getColumn(4).setPreferredWidth(20);
        tabelaItens.getColumnModel().getColumn(5).setPreferredWidth(20);
        tabelaItens.getColumnModel().getColumn(6).setPreferredWidth(20);
        modeloDocItem =(DefaultTableModel) tabelaItens.getModel();

        txtDocumento.setText("Documento");

        campoDocumento.setEditable(false);

        txtVlTotal.setText("Valor Total");

        campoVlTotal.setEditable(false);

        txtOrigem.setText("Origem");

        campoOrigem.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 740, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtVlTotal)
                    .addComponent(txtDocumento))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(campoDocumento, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                    .addComponent(campoVlTotal))
                .addGap(18, 18, 18)
                .addComponent(txtOrigem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDocumento)
                    .addComponent(campoDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtOrigem)
                    .addComponent(campoOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtVlTotal)
                    .addComponent(campoVlTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void buscaInfos() {
        BDDocumentoEmp bDDocumento = new BDDocumentoEmp();
        Tabela.limparTabela(modeloDocItem);
        try {
            Documento doc = null;
            if (origem.contains("NFE")) {
                doc = bDDocumento.docNFEntradas(documento);
                Item.listaItens(doc.getItens(), modeloDocItem);
            } else if (origem.contains("NFV")) {
                doc = bDDocumento.docNFSaidas(documento);
                Item.listaItens(doc.getItens(), modeloDocItem);
            } else if (origem.contains("NFO")) {
                doc = bDDocumento.docNFOutras(documento);
                Item.listaItens(doc.getItens(), modeloDocItem);
            }
            campoDocumento.setText(doc.getNumero() + "");
            campoOrigem.setText(doc.getOrigem());
            campoVlTotal.setText(doc.getValor() + "");
        } catch (SQLException erro) {
            JOptionPane.showMessageDialog(null, erro);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoDocumento;
    private javax.swing.JTextField campoOrigem;
    private javax.swing.JTextField campoVlTotal;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaItens;
    private javax.swing.JLabel txtDocumento;
    private javax.swing.JLabel txtOrigem;
    private javax.swing.JLabel txtVlTotal;
    // End of variables declaration//GEN-END:variables
}
